package com.viewpagerindicator.sample.adapters;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.viewpagerindicator.sample.GoodsDetailActivity;
import com.viewpagerindicator.sample.R;
import com.viewpagerindicator.sample.beans.Goods;
import com.viewpagerindicator.sample.util.ImageLoaderUtils;

import java.math.BigDecimal;
import java.util.List;


/**
 * Created by zhao on 2015/11/7.
 */
public class GoodsListAdapter extends ArrayAdapter<Goods> {
    private int resource;
    private LayoutInflater inflater;
    private ImageLoader imageLoader;
    public GoodsListAdapter(Context context, int resource, List<Goods> objects) {
        super(context, resource, objects);
        this.resource=resource;
        this.inflater=LayoutInflater.from(context);
        imageLoader= ImageLoaderUtils.getInstance(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView==null){
            convertView = inflater.inflate(resource, null);
        }
        TextView textView= (TextView) convertView.findViewById(R.id.goods_item_name);
        textView.setText(getItem(position).getName());

        TextView textViewdesc= (TextView) convertView.findViewById(R.id.goods_item_desc);
        textViewdesc.setText(getItem(position).getDesc());

        TextView textViewmark= (TextView) convertView.findViewById(R.id.goods_item_mark);
        textViewmark.setText(getItem(position).getRemark());


        TextView textViewPrice= (TextView) convertView.findViewById(R.id.goods_item_price);
        BigDecimal price=getItem(position).getPrice();
        if(price.compareTo(new BigDecimal(0))==0){
            textViewPrice.setTextColor(getContext().getResources().getColor((R.color.price_red)));
            textViewPrice.setText(getContext().getString(R.string.goods_price_tip));
        }else{
            textViewPrice.setTextColor(getContext().getResources().getColor((R.color.price_blue)));
            textViewPrice.setText("$"+price.toString());
        }
        convertView.setTag(getItem(position).getId());
        Button button= (Button) convertView.findViewById(R.id.goods_item_button);
        button.setTag(getItem(position).getId());
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Integer goodsId = (Integer) view.getTag();
                Intent intent = new Intent(getContext(), GoodsDetailActivity.class);
                intent.putExtra("goodsid", goodsId + "");
                ((Activity) getContext()).startActivity(intent);
            }
        });
        ImageView imageView= (ImageView) convertView.findViewById(R.id.goods_thumb);
        imageLoader.displayImage(getItem(position).getThumb(),imageView);
        return convertView;
    }
}
